package com.util;

import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduledThreadPoolExampleUtil {
    public static void main(String[] args) throws InterruptedException {
        // 创建 ScheduledThreadPool 线程池
        ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(10);
        System.out.println("scheduleAtFixedRate 方法添加任务：" + LocalDateTime.now());
        threadPool.scheduleAtFixedRate(new Runnable() {
                                           @Override
                                           public void run() {
                                               System.out.println("执行 scheduleAtFixedRate 方法：" + LocalDateTime.now());
                                               JsoupUtil.getDate();
                                               // 休眠 2s
                                               try {
                                                   TimeUnit.SECONDS.sleep(2);
                                               } catch (InterruptedException e) {
                                                   e.printStackTrace();
                                               }
                                           }
                                       },
                3L, // 3s 后开始执行定时任务
                86400L, // 定时任务的执行间隔为 24小时
                TimeUnit.SECONDS); // 描述上面两个参数的时间单位
    }
}
